package hu.kisprof.quiz.controller;

import hu.kisprof.quiz.model.QuizDAO;
import hu.kisprof.quiz.model.bean.Edu;
import hu.kisprof.quiz.model.bean.Player;
import hu.kisprof.quiz.model.bean.Question;
import hu.kisprof.quiz.model.bean.Statistic;
import hu.kisprof.quiz.model.bean.Topic;
import hu.kisprof.quiz.view.Labels;
import hu.kisprof.quiz.view.QuizGUI;
import hu.kisprof.quiz.view.dialogs.QuizChallengeReceivedDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import javax.swing.table.DefaultTableModel;

public class QuizController {
	private QuizDAO dao = new QuizDAO();
	private QuizServerConnectionListener cListener= new QuizServerConnectionListener(this);
	private QuizGUI gui;
	private Player currentPlayer = null;
	private String currentRoom = null;
	private boolean inGame = false;
	private String inGameEnemy=null;
	
	
	public QuizController(){
		cListener.start();
	}

	
	public static void main(String[] args) {		
		QuizController control = new QuizController();
		control.startDesktop();
		

	}
	
	private void startDesktop() {
		gui = new QuizGUI(this);
		gui.startGUI();


	}

	public boolean registerPlayer(Player player) {
		return dao.registerPlayer(player);
		
	}

	public boolean login(String usrname, String string) {
		currentPlayer = dao.login(usrname,string,cListener.getPort());
		if(currentPlayer==null){
			return false;
		}
		return true;
		
	}
		
	public Player getPlayer(){
		//currentPlayer=dao.updatePlayer(currentPlayer.getUsername());
		return currentPlayer;
	}

	public Map<String, Integer> getRooms() {
		return dao.getRooms();
	}

	public String getCurrentRoom() {
		return currentRoom;
	}


	public boolean createRoom(String roomName) {
		return dao.createRoom(roomName);
	}

	public TreeSet<String> getRoomPlayers(String roomName) {
		return dao.getRoomPlayers(roomName);
	}

	public void joinRoom(String roomName) {
		if(dao.joinRoom(roomName,currentPlayer)){
			currentRoom=roomName;
		}
		
	}
	
	public void leaveRoom(){
		dao.leaveRoom(currentRoom,currentPlayer);
		currentRoom = null;
	}

	public void logout() {
		dao.logout(currentPlayer.getUsername());
		this.currentPlayer = null;
	}

	public List<Statistic> getPlayerStats(String username) {
		List<Statistic> stList = new ArrayList<Statistic>();
			Player ply = dao.getPlayer(username);		
			stList.add(new Statistic(Labels.realName,ply.getName()));
			stList.add(new Statistic(Labels.email,ply.getEmail()));
			stList.add(new Statistic(Labels.age,ply.getAge()+""));
			stList.add(new Statistic(Labels.gender,ply.getGender()?Labels.male:Labels.female));
			stList.add(new Statistic(Labels.score,ply.getPoint()+""));
			stList.add(new Statistic(Labels.userType,ply.getType()==0?Labels.user:Labels.admin));
		
		
		return stList;
}

	public boolean challangePlayer(String username, String message, int difficulty) {
		return dao.challangePlayer(currentPlayer.getUsername(),username, message, difficulty);
		
	}

	public boolean anonymousLogin() {
		currentPlayer = dao.anonymousLogin();
		if(currentPlayer==null){
			return false;
		}
		return true;
	}
	public QuizGUI getGUI(){
		return gui;
	}

	public void setChallengeState(boolean readBoolean) {
		dao.setChallengeState(readBoolean);
	}


	public boolean challengeReceived(String username, String message, int difficulty) {
		if(inGame){
			return false;
		}else{
			QuizChallengeReceivedDialog d = new QuizChallengeReceivedDialog(username, message, difficulty, gui);
			boolean bol = d.showDialog();
			return bol;
		}
		
	}


	public boolean isPlayerOnline(String username) {
		return dao.isPlayerOnline(username);
	}
	
	public boolean isInGame(){
		return inGame;
	}
	
	public void setInGame(String enemyUsername, boolean b){
		if(b==true){
			this.inGameEnemy=enemyUsername;
		}
		inGame=b;
		
	}
	public void setInGame(boolean b){
		inGame=b;
		if(b==false){
			this.inGameEnemy=null;
		}
	}

	public void playerReady(boolean answer) {
		dao.playerReady(currentPlayer.getUsername(),answer);
		
	}


	public TreeSet<String> getQuestions() {
		return dao.getQuestions();
	}


	public TreeSet<Topic> getTopics() {
		return dao.getTopics();
	}


	public void addTopic(String name) {
		dao.addTopic(name);
		
	}


	public void addNewQuestion(String question, String good, TreeSet<Topic> topics, int difficulty, TreeSet<String> badAnswers){
		dao.addNewQuestion(question, good,topics,difficulty,badAnswers);
		
	}


	public boolean setServer(String serverAddr) {
		return dao.setServer(serverAddr);		
	}


	public TreeSet<Edu> getEdus() {
		return dao.getEdus();
	}


	public String checkZipCode(String zipcode) {
		try{
			String tmp= dao.checkZipCode(Integer.parseInt(zipcode));
			if(tmp.equals("")){
				return null;
			}else{
				return tmp;
			}
		}catch(NumberFormatException e){
			return null;			
		}
	}


	public void deleteQuestion(String id) {
		try{
			dao.deleteQuestion(Integer.parseInt(id));
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
	}


	public void editQuestion(int questionId, String question, String goodAns,
			TreeSet<Topic> topics, int difficulty, TreeSet<String> badAnswers) {
		
		dao.editQuestion(questionId,question,goodAns,topics, difficulty,badAnswers); 
		
	}


	public Question getQuestion(String id) {
		try{
			return dao.getQuestion(Integer.parseInt(id));
		}catch(NumberFormatException e){
			e.printStackTrace();
			return null;
		}

	}


	public int getPoints(String plyName) {
		int i=dao.getPoints(plyName);
		return i;
	}


	public String getInGameEnemy() {
		return inGameEnemy;
	}


	public void setInGameEnemy(String inGameEnemy) {
		this.inGameEnemy = inGameEnemy;
	}


	public DefaultTableModel getStatistic(int i) {
		return dao.getStatistic(i);
	}


	public TreeSet<String> getAllPlayers() {
		return dao.getAllPlayers();
	}


	public void setAdmin(String username) {
		dao.setAdmin(username);
		
	}


	public void deletePlayer(String username) {
		dao.deletePlayer(username);
		
	}
}
